﻿using System;
using System.Collections.Generic;
using System.Threading;
using TeamTasker.Contracts;
using TeamTasker.Utils;

namespace TeamTasker.Server.BusinessLogic.Staff
{
    public class StaffRepository
    {
        /*
        /// <summary>
        /// Refresh interval, in seconds, of a users list from external repository.
        /// </summary>
        public int RefreshInterval { get; set; }

        /// <summary>
        /// Timer for user repositories synchronization; 
        /// </summary>
        private Timer SyncTimer = null;
        */

        /// <summary>
        /// Connector to external repository. 
        /// </summary>
        public IStaffRepositoryConnector StaffRepoConnector { get; set; }

        /// <summary>
        /// Reference to User facade.
        /// </summary>
        public UserFacade UserFacade { get; set; }

        /// <summary>
        /// Checks if user exists in staff repository. 
        /// </summary>
        /// <param name="login">user login ID</param>
        /// <param name="password">user password</param>
        /// <returns>boolean indication of success</returns>
        public bool CheckUser(string login, string password) 
        {
            return StaffRepoConnector.CheckUser(login, password);
        }

        /// <summary>
        /// Returns all users from the external repository.
        /// </summary>
        /// <returns>list of user entries</returns>
        public IList<User> GetAllUsers() 
        {
            return StaffRepoConnector.GetAllUsers();
        }

        /// <summary>
        /// Returns users from the external repository modified after specified timestamp.
        /// </summary>
        /// <param name="since">threshold timestamp</param>
        /// <returns>list of user entries</returns>
        public IList<User> GetModifiedUser(DateTime since) 
        {
            return StaffRepoConnector.GetModifiedUser(since);
        }

        public void SynchronizeUserList()
        {
            // TODO: introduce "GetModifiedUsers" in case if it's not the first sync.
            IList<User> users = StaffRepoConnector.GetAllUsers();
            foreach (User user in users) 
            {
                UserFacade.Save(user);
            }
            // TODO - deactivate other users which are in our database.
        }

        /*
        /// <summary>
        /// Timer event handler. 
        /// </summary>
        /// <param name="stateInfo"></param>
        public void OnTimerFire(Object stateInfo)
        {
            SynchronizeUserList();
        }
        */
    }
}
